﻿//
// PicoMax
// Copyright 2012 Thomas W. Holtquist
// www.skewworks.com
//
// GPL 3.0 Licensed
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace SpiralImageConverter
{
    public partial class frmMain : Form
    {

        #region Form Code

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            frmMain_Resize(sender, e);
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            btnBrowse.Left = this.Width - btnBrowse.Width - 20;
            txtOutput.Width = btnBrowse.Left - txtOutput.Left - 10;
            btnConvert.Left = this.Width - btnConvert.Width - 20;
        }

        #endregion

        #region Objects

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (FBD.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;

            txtOutput.Text = FBD.SelectedPath;
            CheckButtons();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (OFD.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;

            ListViewItem LVI;
            for (int i = 0; i < OFD.FileNames.Length; i++)
            {
                LVI = new ListViewItem(Path.GetFileName(OFD.FileNames[i]));
                LVI.Tag = OFD.FileNames[i];
                LVI.SubItems.Add("Ready");
                lstFiles.Items.Add(LVI);
            }

            CheckButtons();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            while (lstFiles.SelectedItems.Count > 0)
                lstFiles.Items.Remove(lstFiles.SelectedItems[0]);
            CheckButtons();
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            lstFiles.Items.Clear();
            CheckButtons();
        }

        private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckButtons();
        }

        private void txtOutput_TextChanged(object sender, EventArgs e)
        {
            CheckButtons();
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            for (int i = 0; i < lstFiles.Items.Count; i++)
                Convert(lstFiles.Items[i]);
            this.Enabled = true;
        }

        #endregion

        #region Private Methods

        private void CheckButtons()
        {
            bool bEnable = true;
            if (txtOutput.Text == string.Empty)
                bEnable = false;
            if (lstFiles.Items.Count == 0)
                bEnable = false;
            btnConvert.Enabled = bEnable;

            btnRemove.Enabled = lstFiles.SelectedItems.Count > 0;
            btnRemoveAll.Enabled = lstFiles.Items.Count > 0;
        }

        private ushort Color16FromRGB(Color c)
        {
            //return (ushort)((c.R >> 3) | ((c.G & 0xFC) << 3) | ((c.B & 0xF8) << 8));
            return (ushort)((c.B >> 3) | ((c.G & 0xFC) << 3) | ((c.R & 0xF8) << 8));
        }

        private void Convert(ListViewItem LVI)
        {
            try
            {
                LVI.SubItems[1].Text = "Converting...";
                string filename = (string)LVI.Tag;
                string outputFile = (string)(txtOutput.Text.EndsWith("\\") ? txtOutput.Text : txtOutput.Text + "\\") + LVI.Text.Replace(Path.GetExtension(LVI.Text), ".bin");

                Bitmap bmpSource = new Bitmap(filename);
                int x, y;
                ushort c16;
                byte[] bOut = new byte[2];

                FileStream iFile = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.Write);

                bOut[0] = (byte)(bmpSource.Width >> 8);
                bOut[1] = (byte)(bmpSource.Width);
                iFile.Write(bOut, 0, 2);

                bOut[0] = (byte)(bmpSource.Height >> 8);
                bOut[1] = (byte)(bmpSource.Height);
                iFile.Write(bOut, 0, 2);

                for (y = 0; y < bmpSource.Height; y++)
                {
                    for (x = 0; x < bmpSource.Width; x++)
                    {
                        c16 = Color16FromRGB(bmpSource.GetPixel(x, y));

                        bOut[1] = (byte)(c16 >> 8);
                        bOut[0] = (byte)(c16);

                        iFile.Write(bOut, 0, 2);
                    }
                }
                iFile.Close();
                LVI.SubItems[1].Text = "Converted";
            }
            catch (Exception) { }
        }

        #endregion

    }
}
